package actions;

import gui.MainFrame;
import gui.view.GroupableTableColumnModel;
import gui.view.GroupableTableHeader;

import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import tablemodels.ExpertCompetenceModel;
import utils.ColumnGroup;
import utils.Expert;
import utils.SimpleColumn;

/**
 * 
 * @author dernasherbrezon
 *
 */
public class CompetenceOfExpertFiller extends WizardStep
{
	private MainFrame parent;
	private ArrayList<ExpertCompetenceModel> data = new ArrayList<ExpertCompetenceModel>();
	
	public CompetenceOfExpertFiller(MainFrame parent)
	{
		this.parent = parent;
		BoxLayout bl = new BoxLayout(this,BoxLayout.Y_AXIS);
		setLayout(bl);
	}
	
	@Override
	public void checkInputValues() throws Exception
	{
		for(int i=0;i<data.size();i++)
		{
			data.get(i).checkCorrectValues();
		}
	}

	@Override
	public void makeActions()
	{
		int[][] summData = new int[parent.expertCriterions.size()][parent.experts.size()];
		for(int i=0;i<parent.experts.size();i++)
		{
			for(int j=0;j<parent.expertCriterions.size();j++)
			{
				summData[i][j] = calculateSummOfCurrentCriterionAndExpert(i,j);
			}
		}
		
		float[] expertRang = new float[parent.experts.size()];
		for(int i=0;i<parent.experts.size();i++)
		{
			for(int j=0;j<parent.expertCriterions.size();j++)
			{
				expertRang[i] += summData[i][j]*parent.expertCriterions.get(j).weight;
			}
		}
		
		float summRanks = 0.0f;
		for(int i=0;i<expertRang.length;i++)
		{
			summRanks += expertRang[i];
		}
		
		float summ = 0;
		for(int i=0;i<parent.experts.size();i++)
		{
			parent.experts.get(i).weight = (float)expertRang[i]/summRanks;
			summ += parent.experts.get(i).weight;
		}
		
		//summ must equals 1!
	}
	
	private int calculateSummOfCurrentCriterionAndExpert(int expertIndex,int criterionIndex)
	{
		int retValue = 0;
		for(int i=0;i<data.size();i++)
		{
			retValue += (Integer)data.get(i).getValueAt(expertIndex, criterionIndex);
		}
		return retValue;
	}

	@Override
	public void loadStep()
	{
		removeAll();
		for(int i=0;i<parent.experts.size();i++)
		{
			createTableForExpert(parent.experts.get(i));
		}
	}

	private void createTableForExpert(Expert expert)
	{
		add(new JLabel("Оценка компетентности экспертов: "+expert.name));
		ExpertCompetenceModel tableModel = new ExpertCompetenceModel(parent);
		data.add(tableModel);
		
		
		
        GroupableTableColumnModel cm = new GroupableTableColumnModel();
        
        for(int i=0;i<tableModel.getColumnCount();i++)
        {
            SimpleColumn Column0 = new SimpleColumn(i,tableModel.getColumnName(i));
            cm.addColumn(Column0);
        }
        
        ColumnGroup crit = new ColumnGroup("Критерии");
        for(int i=0;i<parent.expertCriterions.size();i++)
        {
        	crit.add(cm.getColumn(1+i));
        }
        cm.addColumnGroup(crit);
		
		
		
		JTable table = new JTable(tableModel);
		table.getTableHeader().setReorderingAllowed(false);
        table.setAutoCreateColumnsFromModel(false);
        table.setColumnModel(cm);
        table.setTableHeader(new GroupableTableHeader(cm));
		JScrollPane sp = new JScrollPane(table);

		add(sp);
	}

	@Override
	public void preShow()
	{
	}
}
